import 'package:flutter/material.dart';
import 'package:hello_flutter/base/index.dart';
import 'package:hello_flutter/widgets/layout_log.dart';

class FittedBoxWidget extends BasePage {
  const FittedBoxWidget({super.key});

  @override
  String getPageTitle() => 'FittedBox';

  @override
  Widget buildBody(BuildContext context) => Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            gdContainer(BoxFit.none),
            const Text("guodongAndroid"),
            gdContainer(BoxFit.contain),
            const Text("guodongFlutter"),
            const Padding(
              padding: EdgeInsets.symmetric(vertical: 8),
              child: Divider(),
            ),
            LayoutLogPrint(
              tag: 1,
              child: gdRow(' 90000000000000000 '),
            ),
            FittedBox(
              child: LayoutLogPrint(
                tag: 2,
                child: gdRow(' 90000000000000000 '),
              ),
            ),
            gdRow(' 800 '),
            FittedBox(
              child: gdRow(' 800 '),
            ),
            const Padding(
              padding: EdgeInsets.symmetric(vertical: 8),
              child: Divider(),
            ),
            gdRow(' 90000000000000000 '),
            singleLinerFittedBox(LayoutLogPrint(
              tag: 3,
              child: gdRow(' 90000000000000000 '),
            )),
            gdRow(' 800 '),
            singleLinerFittedBox(LayoutLogPrint(
              tag: 4,
              child: gdRow(' 800 '),
            )),
          ],
        ),
      );

  Widget gdContainer(BoxFit boxFit) {
    return Container(
      width: 50,
      height: 50,
      color: Colors.red,
      child: FittedBox(
        fit: boxFit,
        child: Container(
          width: 60,
          height: 70,
          color: Colors.blue,
        ),
      ),
    );
  }

  Widget gdRow(String text) {
    Widget child = Text(text);
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [child, child, child],
    );
  }

  Widget singleLinerFittedBox(Widget child) {
    return LayoutBuilder(builder: (_, constraints) {
      return FittedBox(
        fit: BoxFit.contain,
        child: ConstrainedBox(
          constraints: constraints.copyWith(
            // 第一个版本
            // maxWidth: constraints.maxWidth,

            // 第二个版本
            minWidth: constraints.maxWidth,
            maxWidth: double.infinity,
          ),
          child: child,
        ),
      );
    });
  }
}
